package com.xinchao.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xinchao.constant.AdvertisementConstant;
import com.xinchao.constant.PageConstant;
import com.xinchao.dao.AdvertisementMapper;
import com.xinchao.exception.ServiceException;
import com.xinchao.model.Advertisement;
import com.xinchao.model.dto.AdvertisementListDTO;
import com.xinchao.model.dto.AdvertisementMonitorSumDTO;
import com.xinchao.model.page.PageInfo;
import com.xinchao.model.vo.AdvertisementListVO;
import com.xinchao.model.vo.OverviewAdvertisementVO;
import com.xinchao.service.AdvertisementMonitorService;
import com.xinchao.service.AdvertisementService;
import com.xinchao.service.AdvertiserService;
import com.xinchao.service.ChargeService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author dxy
 * @date 2018/9/18 18:35
 */
@Service(value = "advertisementService")
public class AdvertisementServiceImpl implements AdvertisementService {
	@Autowired
	private AdvertisementMapper advertisementMapper;
	@Autowired
	private AdvertiserService advertiserService;
	@Autowired
	private AdvertisementMonitorService advertisementMonitorService;

	/**
	 * 获取广告列表
	 *
	 * @param advertiserName      广告主名称
	 * @param advertisementName   广告名称
	 * @param advertisementStatus 广告状态
	 * @param sex                 性别
	 * @param age                 年龄
	 * @param pageNum             当前页
	 * @param pageSize            每页条数
	 * @return PageInfo<AdvertisementListDTO>
	 */
	@Override
	public PageInfo<AdvertisementListVO> listAdvertisement(String advertiserName, String advertisementName, Integer advertisementStatus,
	                                                       Integer sex, String age, Integer pageNum, Integer pageSize) throws ServiceException {
		//设置默认值
		if (pageNum == null) {
			pageNum = PageConstant.DEFAULT_PAGE_NUM;
		}
		if (pageSize == null) {
			pageSize = PageConstant.DEFAULT_PAGE_SIZE;
		}
		PageInfo<AdvertisementListVO> pageInfo = new PageInfo<>();
		pageInfo.setPageNum(pageNum);
		pageInfo.setPageSize(pageSize);
		//广告主ID列表
		List<Long> advertiserIds = null;
		if (StringUtils.isNotBlank(advertiserName)) {
			advertiserIds = advertiserService.listAdvertiserIdByAdvertiserName(advertiserName);
			//如果没有查询到广告主ID，直接返回空列表的PageInfo
			if (CollectionUtils.isEmpty(advertiserIds)) {
				pageInfo.setTotal(0L);
				pageInfo.setList(new ArrayList<>());
				return pageInfo;
			}
		}
		PageHelper.startPage(pageNum, pageSize);
		Page<AdvertisementListDTO> page = advertisementMapper.listAdvertisement(advertiserIds, advertisementName, advertisementStatus, sex, age);
		if (page == null) {
			pageInfo.setTotal(0L);
			pageInfo.setList(new ArrayList<>());
			return pageInfo;
		}
		//计算广告已消费费用
		List<AdvertisementListDTO> dtoList = page.getResult();
		List<AdvertisementListVO> voList = new ArrayList<>();
		if (CollectionUtils.isNotEmpty(dtoList)) {
			for (AdvertisementListDTO dto : dtoList) {
				String advertisementCode = dto.getAdvertisementCode();
				if (StringUtils.isBlank(advertisementCode)) {
					continue;
				}
				AdvertisementListVO vo = new AdvertisementListVO();
				vo.setAdvertiserName(dto.getAdvertiserName());
				vo.setAdvertisementName(dto.getAdvertisementName());
				vo.setAge(dto.getAge());
				vo.setSex(dto.getSex());
				//查询广告的总观看人次
				Long watchNumberSum = advertisementMonitorService.sumWatch(dto.getAdvertisementName(), null, null);
				//已消费金额（广告A的消费金额=广告A的观看人次*￥0.01）
				if (watchNumberSum == null) {
					vo.setConsumedCharge(0);
				} else {
					BigDecimal consumedCharge = new BigDecimal(watchNumberSum).multiply(AdvertisementConstant.PER_ADVERTISEMENT_CHARGE);
					double doubleValue = consumedCharge.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
					vo.setConsumedCharge(doubleValue);
				}
				voList.add(vo);
			}
		}
		pageInfo.setTotal(page.getTotal());
		pageInfo.setList(voList);
		return pageInfo;
	}

	/**
	 * 通过广告名称获取广告
	 *
	 * @param advertisementName 广告名称
	 * @return Advertisement
	 */
	@Override
	public Advertisement getAdvertisementByAdvertisementName(String advertisementName) throws ServiceException {
		if (StringUtils.isBlank(advertisementName)) {
			throw new ServiceException("广告名称不能为空");
		}
		return advertisementMapper.getAdvertisementByAdvertisementName(advertisementName);
	}

	/**
	 * 通过广告主ID获取广告编码
	 * @param advertiserId 广告主ID
	 * @return List<String>
	 * @throws ServiceException
	 */
	@Override
	public List<String> listAdvertisementCode(Long advertiserId) throws ServiceException {
		if (advertiserId == null) {
			throw new ServiceException("广告主ID不能为空");
		}
		return advertisementMapper.listAdvertisementCode(advertiserId);
	}

}
